﻿Public Class frmNewUsageHistory
    Dim Strsql As String
    Public Primary_code As Integer
    Dim _mysql As New ClsSQLhelper
#Region "Function"
    Sub Load_Department()
        Try
            Strsql = "Select Department_ID,Department_Name from department"
            Dim DT_Budget As New DataTable
            DT_Budget = _mysql.GetMYSQLDataTable(Strsql, "department")
            With Me.CboDepartment
                .DataSource = DT_Budget
                .DisplayMember = "Department_Name"
                .ValueMember = "Department_ID"
            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Department"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try

    End Sub
    Function Validate_data() As Boolean
        Validate_data = False
        Try
           

            Strsql = "Select count(*) as cc from historyusage where historyusage_itemid = " & Primary_code
            Strsql = Strsql & " and historyusage_department = " & CboDepartment.SelectedValue & " and historyusage_startdate = '"
            Strsql = Strsql & Pn_Framework.MysqlDateTimeFormat(Me.DtpStartDate.DateTime, False) & "' "


            Dim DupplicateID As Integer = _mysql.MySQLExecuteScalar(Strsql)
            Select Case DupplicateID
                Case Is > 0
                    MsgBox("ประวัติที่ใช้ในหน่วยงานนี้และวันเริ่มต้นใช้งานนี้มีอยู่แล้ว", MsgBoxStyle.Critical, "ไม่สามารถเพิ่มข้อมูลได้")
                    Exit Function
                Case -1
                    With frmDebug
                        .lblFormName.Text = Me.Name
                        .lblFunctionName.Text = "Validate_data"
                        .MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                        .MemoSQL.Text = Strsql
                        .ShowDialog()
                    End With
            End Select


            Return True
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Function
    Sub Insert_Data()
        Try
            Strsql = "Insert into historyusage (historyusage_itemid,historyusage_department,historyusage_startdate,historyusage_remarks)"
            Strsql = Strsql & " values(" & Primary_code & "," & CboDepartment.SelectedValue & ",'" & Pn_Framework.MysqlDateTimeFormat(Me.DtpStartDate.DateTime, False)
            Strsql = Strsql & "','" & Me.MemoRemark.Text & "')"



            Dim ExecuteResualt As Integer = _mysql.MySQLExecute(Strsql)
            Select Case ExecuteResualt
                Case Is > 0
                    MsgBox("เพิ่มประวัติการใช้งานเรียบร้อยแล้ว", MsgBoxStyle.Information, "ผลการเพิ่มข้อมูล")
                    Exit Sub
                Case -1
                    With frmDebug
                        .lblFormName.Text = Me.Name
                        .lblFunctionName.Text = "OK_Button_Click"
                        .MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                        .MemoSQL.Text = Strsql
                        .ShowDialog()
                    End With
            End Select

        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Insert_Data"
                .MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub

#End Region

    Private Sub BtnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnOK.Click
        If Validate_data() = False Then Exit Sub
        Insert_Data()
        DialogResult = Windows.Forms.DialogResult.OK
    End Sub

    Private Sub BtnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub frmNewUsageHistory_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Load_Department()
        Me.DtpStartDate.DateTime = Now.Date

    End Sub

    Private Sub GroupControl2_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles GroupControl2.Paint

    End Sub
End Class